<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">

      <img src="" />

      <el-tab-pane label="单商品添加" name="second">
        <div style="width: 800px;margin-left: 50px">
          <el-form ref="form" :model="form"  :rules="rules" label-width="120px">
            <el-form-item label="视频号小店账号" prop="accountId">
              <el-select clearable v-model="form.accountId">
                <el-option
                  v-for="(item,index) of accountList"
                  :key="index"
                  :label="item.nickname"
                  :value="item.id"
                >{{ item.nickname }}
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="佣金比" prop="ratio">
              <el-input v-model="form.ratio" type="number" placeholder="请输入佣金率"/>
            </el-form-item>
            <el-form-item label="商品id" prop="spuId">
              <el-input v-model="form.spuId" placeholder="请输入商品id,多个商品id用，分隔"/>
            </el-form-item>
          </el-form>
          <div style="margin-top: 80px;float: right">
            <el-button :loading="buttonLoading" type="primary" @click="createShopData">确 定</el-button>
          </div>
        </div>
      </el-tab-pane>


      <el-tab-pane label="商品列表批量添加" name="batchAdd">
        <div style="height: 1200px;width: 1000px;">
          <div v-loading="buttonLoading" style="width: 800px;margin-left: 50px">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
              <el-form-item label="视频号小店账号" prop="accountId">
                <el-select clearable v-model="form.accountId">
                  <el-option
                    v-for="(item,index) of accountList"
                    :key="index"
                    :label="item.nickname"
                    :value="item.id"
                  >{{ item.nickname }}
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="佣金比" prop="ratio">
                <el-input v-model="form.ratio" type="number" placeholder="请输入佣金率"/>
              </el-form-item>
            </el-form>
            <div style="margin-top: 80px;float: right">

              <el-button :loading="buttonLoading" type="primary" @click="batchAddShop">确 定</el-button>
            </div>
          </div>
          <div
            style="font-size: 20px;color: #1c84c6;position: absolute;left: 150px;width: 450px;height: 500px;overflow:auto;">
            {{ resMsg }}
          </div>
        </div>
      </el-tab-pane>


      <el-tab-pane label="定向和专属推广" name="asd">
        <div style="height: 1200px;width: 1500px;">
          <div v-loading="buttonLoading" style="width: 800px;margin-left: 50px">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">

              <el-form-item label="视频号小店账号" prop="accountId">
                <el-select clearable v-model="form.accountId">
                  <el-option
                    v-for="(item,index) of accountList"
                    :key="index"
                    :label="item.nickname"
                    :value="item.id"
                  >{{ item.nickname }}
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="开始时间" prop="startTime">
                  <el-date-picker v-model="form.startTime" type="datetime" placeholder="请选择开始时间"></el-date-picker>
                  </el-form-item>
                  <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker v-model="form.endTime" type="datetime" placeholder="请选择结束时间" ></el-date-picker>
               </el-form-item>

               <el-form-item label="推广类型" prop="promotionType">
              <el-select v-model="form.promotionType">
                <el-option label="定向推广" :value="2"></el-option>
                <el-option label="专属推广" :value="3"></el-option>
              </el-select>
            </el-form-item>


                <div>
                  <el-button  @click="addItem()">新增</el-button>
                </div>

                  <div v-for="(item, index) in infoList" :key="index" style="display: flex; align-items: center;">
                    <el-form-item label="商品id" prop="spuId">
                      <el-input v-model="item.spuId" type="number" placeholder="请输入商品id"/>
                    </el-form-item>
                    <el-form-item label="推广账号id" prop="finderUinList">
                      <el-input v-model="item.finderUinList" type="number" placeholder="请输入推广账号id"/>
                    </el-form-item>

                    <el-form-item>
                      <el-button type="danger" v-if="index !== 0" style="margin-bottom: 5px;"  @click="removeItem(index)">移除</el-button>
                    </el-form-item>
                  </div>




            </el-form>
            <div style="margin-top: 80px;float: right">
              <el-button :loading="buttonLoading" type="primary" @click="exclusiveInfoData">确 定</el-button>
            </div>
          </div>
          <div
            style="font-size: 20px;color: #1c84c6;position: absolute;left: 150px;width: 450px;height: 500px;overflow:auto;">
            {{ resMsg }}
          </div>
        </div>
      </el-tab-pane>


    </el-tabs>


  </div>
</template>

<script>
import {
  listChannelsMail,
  getChannelsMail,
  delChannelsMail,
  addChannelsMail,
  updateChannelsMail,
  createShop,
  batchAdd,
  exclusiveInfo
} from "@/api/channelsMail/channelsMail";
import {
  getChannelsMall
} from "@/api/media/mediaAccount";
export default {
  name: "ChannelsMail",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      activeName: 'second',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //列表
      infoList: [{spuId: '', finderUinList: ''}],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      accountList:[],
      // 视频号小店开通表格数据
      // channelsMailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        userId: undefined,
        cookies: undefined,
        spuId: undefined
      },
      resMsg: '',
      // 表单参数
      form: {
        ratio: 1
      },
      // 表单校验
      rules: {
        id: [
          {required: true, message: "主键不能为空", trigger: "blur"}
        ],
        startTime: [
          {required: true, message: "开始时间不能为空", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ],
              checkTime: [
          {validator: (rule, value, callback) => {
            if (value && this.form.endTime && value > this.form.endTime) {
              callback(new Error('开始时间不能大于结束时间'));
            } else {
              callback();
            }
          }, trigger: 'blur'}
        ],
        promotionType:[
        {required: true, message: "推广类型不能为空", trigger: "blur"}
        ],
        accountId:[
          {required: true, message: "账号不能为空", trigger: "blur"}
        ],
        ratio: [
          {required: true, message: "请输入佣金比", trigger: "blur"}
        ],
        // finderUinList:[{
        //   required: true, message: "请输入账号id", trigger: "blur"
        // }],
        // spuId: [
        //   {required: true, message: "商品id不能为空", trigger: "blur"}
        // ],
      }
    };
  },
  created() {
    this.getImg()
    //this.getList();
  },
  methods: {
    /** 查询视频号小店开通列表 */
    getList() {
      this.loading = true;
      getChannelsMall().then(response => {
        this.accountList = response.data
        this.loading = false;
      });
    },


    createShopData() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          console.log(this.form)
          createShop(this.form).then((res) => {
            console.log(res)
            this.$modal.msgSuccess("添加成功！");
          }).finally(() => {
            this.buttonLoading = false;
          })
        }
      })
    },


    /**
     * 添加数据
     */
    addItem(){
        this.infoList.push({spuId: '', finderUinList: ''})
    },

    removeItem(index){
      this.infoList.splice(index, 1);
    },


    /**
     * 根据类型选择
     */
    exclusiveInfoData(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.infoList = JSON.stringify(this.infoList)
          exclusiveInfo(this.form).then((res) => {
            console.log(res)
            this.resMsg = res.msg
            this.$modal.msgSuccess("添加成功！");
          }).finally(()=>{
            this.buttonLoading = false;
          })
        }
      })
    },

    /**
     * 批量添加
     */
    batchAddShop() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          batchAdd(this.form).then((res) => {
            this.resMsg = res.msg
          }).finally(() => {
            this.buttonLoading = false;
          })
        }
      })
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: undefined,
        userId: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        cookies: undefined,
        spuId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频号小店开通";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getChannelsMail(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改视频号小店开通";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateChannelsMail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addChannelsMail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除视频号小店开通编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delChannelsMail(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('channelsMail/channelsMail/export', {
        ...this.queryParams
      }, `channelsMail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
